package com.example.hotelmanagement.controller;

import com.example.hotelmanagement.entity.Reservation;
import com.example.hotelmanagement.entity.Result;
import com.example.hotelmanagement.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/reservations")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    @GetMapping
    public Result getAllReservations() {
        return Result.ok(reservationService.findAll());
    }

    @GetMapping("/{id}")
    public Result getReservationById(@PathVariable Integer id) {
        Reservation reservation = reservationService.findById(id);
        return reservation != null ? Result.ok(reservation) : Result.error();
    }

    @GetMapping("/room/{roomId}")
    public Result getReservationsByRoom(@PathVariable Integer roomId) {
        List<Reservation> reservations = reservationService.findByRoomId(roomId);
        return Result.ok(reservations);
    }

    @GetMapping("/status/{status}")
    public Result getReservationsByStatus(@PathVariable String status) {
        List<Reservation> reservations = reservationService.findByStatus(status);
        return Result.ok(reservations);
    }

    @PostMapping
    public Result createReservation(@RequestBody Reservation reservation) {
        return Result.ok(reservationService.save(reservation));
    }

    @PutMapping
    public Result updateReservation(@RequestBody Reservation reservation) {
        return Result.ok(reservationService.update(reservation));
    }

    @DeleteMapping("/{id}")
    public Result deleteReservation(@PathVariable Integer id) {
        return Result.ok(reservationService.delete(id));
    }
}